home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / SIN.C < prev    next >
Text File  |  1986-05-18  |  3KB  |  91 lines

  1. /* 1.1  07-08-85                        (sin.c) 
  2.  ************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1985        *
  6.  ************************************************************************
  7.  *    Programmmed using the algorithms given in:
  8.  *
  9.  *    Coty, William J., Jr., and Waite, William, SOFTWARE MANUAL FOR
  10.  *    THE ELEMENTARY FUNCTIONS, Prentice-Hall Series in Computational
  11.  *    Mathematics, Prentice-Hall, Inc., Inglewood Cliffs, NJ, 1980, 
  12.  *    pp. 125-149.
  13.  *
  14.  *----------------------------------------------------------------------*/
  15.  
  16. #include "defs.h"
  17. #include "stdtyp.h"
  18. #include "errno.h"
  19. #include "mathtyp.h"
  20. #include "mathcons.h"
  21.  
  22. /*----------------------------------------------------------------------*/
  23.  
  24. #define R0    1.0
  25. #define R1    0.16666666666666665052e+00
  26. #define R2    0.83333333333331650314e-02
  27. #define R3    0.19841269841201840457e-03
  28. #define R4    0.27557319210152756119e-05
  29. #define R5    0.25052106798274584544e-07
  30. #define R6    0.16058936490371589114e-09
  31. #define R7    0.76429178068910467734e-12
  32. #define R8    0.27204790957888846175e-14
  33.  
  34. #define C1    3.1416015625            /* C1 - C2 = PI        */
  35. #define C2    8.9089102067615373566e-6;
  36.  
  37. LOCAL double sincos();
  38.  
  39. /*\p*********************************************************************/
  40.     double
  41. sin(x)                /* return trigonometric sine of x    */
  42.  
  43. /*----------------------------------------------------------------------*/
  44. double x;
  45. {
  46.     if (x < 0.0)
  47.         return sincos(x, -x, 1);
  48.     else
  49.         return sincos(x, x, 0);
  50. }
  51.  
  52. /************************************************************************/
  53.     double
  54. cos(x)                /* return trigonometric cosine of x    */
  55.  
  56. /*----------------------------------------------------------------------*/
  57. double x;
  58. {
  59.     return sincos(x, ABS(x) + PIover2, 0);
  60. }
  61.  
  62. /************************************************************************/
  63.     LOCAL double
  64. sincos(x, y, sgnflag)        /* compute both sine and cosine        */
  65.  
  66. /*----------------------------------------------------------------------*/
  67. double x, y;
  68. {
  69.     double f, xn, r, g;
  70.  
  71.     if (y >= MAXANGLE)
  72.     {    errno = ERANGE;
  73.         return 0.0;
  74.     }
  75.     if (modf(y * INVPI, &xn) >= 0.5)
  76.         ++xn;
  77.     if ((int)xn & 1)
  78.         sgnflag = !sgnflag;
  79.     if (y ISNT ABS(x))
  80.         xn -= 0.5;
  81.     g = modf(ABS(x), &x);
  82.     f = ((x - xn * C1) + g) + xn * C2;
  83.     if (ABS(f) > FADEOUT)
  84.     {    g = f * f;
  85.         r = (((((((R8*g-R7)*g+R6)*g-R5)*g
  86.             +R4)*g-R3)*g+R2)*g-R1)*g;
  87.         f += f * r;
  88.     }
  89.     return (sgnflag ? -f : f);
  90. }
  91.